// XortSettingsDlg.cpp : Implementation of CXortSettingsDlg

#include "stdafx.h"
#include "XortSettingsDlg.h"

static const LPCTSTR g_szRegPath = _T("Software\\XortAddin");
static const LPCTSTR g_szSection = _T("XORT ADDIN");
static const LPCTSTR g_szXCLkey  = _T("XCL"); // contains program to debug
static const LPCTSTR g_szXVMkey  = _T("XVM"); // contains program arguments
static const LPCTSTR g_szDBGkey  = _T("DBG"); // c
static const LPCTSTR g_szXRTkey  = _T("XRT");

// CXortSettingsDlg
CXortSettingsDlg::CXortSettingsDlg()
{
    TCHAR szIni[4096];
    DWORD dwSize = 4096;
    GetModuleFileName( _AtlModule.GetResourceInstance(), szIni, dwSize );
    
    TCHAR *p = _tcsrchr( szIni, _T('.') );
    if( p )
        _tcscpy( p, _T(".ini") );

    m_szINIPath = szIni;

    GetPrivateProfileString( g_szSection, g_szXCLkey, _T(""), szIni, dwSize, m_szINIPath );
    m_csXClPath = szIni;

    GetPrivateProfileString( g_szSection, g_szXVMkey, _T(""), szIni, dwSize, m_szINIPath );
    m_csXVMPath = szIni;

    GetPrivateProfileString( g_szSection, g_szDBGkey, _T(""), szIni, dwSize, m_szINIPath );
    m_csDBGPath = szIni;

    GetPrivateProfileString( g_szSection, g_szXRTkey, _T(""), szIni, dwSize, m_szINIPath );
    m_csProgPath = szIni;
}

CXortSettingsDlg::~CXortSettingsDlg()
{

}

LRESULT CXortSettingsDlg::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
    CAxDialogImpl<CXortSettingsDlg>::OnInitDialog(uMsg, wParam, lParam, bHandled);

    GetDlgItem( IDC_EDIT_XCL_PATH ).SetWindowText( m_csXClPath );
    GetDlgItem( IDC_EDIT_XVM_PATH ).SetWindowText( m_csXVMPath );
    GetDlgItem( IDC_EDIT_DBG_PATH ).SetWindowText( m_csDBGPath );

    bHandled = TRUE;
    return 1;  // Let the system set the focus
}

LRESULT CXortSettingsDlg::OnClickedOK(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
    GetDlgItem( IDC_EDIT_XCL_PATH ).GetWindowText( m_csXClPath );
    GetDlgItem( IDC_EDIT_XVM_PATH ).GetWindowText( m_csXVMPath );
    GetDlgItem( IDC_EDIT_DBG_PATH ).GetWindowText( m_csDBGPath );

    WritePrivateProfileString( g_szSection, g_szXCLkey, m_csXClPath, m_szINIPath );
    WritePrivateProfileString( g_szSection, g_szXVMkey, m_csXVMPath, m_szINIPath );
    WritePrivateProfileString( g_szSection, g_szDBGkey, m_csDBGPath, m_szINIPath );
    WritePrivateProfileString( g_szSection, g_szXRTkey, m_csProgPath, m_szINIPath );

    EndDialog( wID );
    return 0;
}

LRESULT CXortSettingsDlg::OnClickedCancel(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
    EndDialog( wID );
    return 0;
}

LRESULT CXortSettingsDlg::OnBnClickedXclPathBtn(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
    CString csXCL = ShowOFN( TRUE );
    m_csXClPath = csXCL;
    GetDlgItem( IDC_EDIT_XCL_PATH ).SetWindowText( csXCL );
    return 1;
}

LRESULT CXortSettingsDlg::OnBnClickedXvmPathBtn(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
    CString csXVM = ShowOFN( FALSE );
    m_csXVMPath = csXVM;
    GetDlgItem( IDC_EDIT_XVM_PATH ).SetWindowText( csXVM );
    return 1;
}

LRESULT CXortSettingsDlg::OnBnClickedXdbPathBtn(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
    CString csDbg = ShowOFN( FALSE );
    m_csDBGPath = csDbg;
    GetDlgItem( IDC_EDIT_DBG_PATH ).SetWindowText( csDbg );
    return 1;
}

CString CXortSettingsDlg::ShowOFN( BOOL bXRT )
{
    OPENFILENAME ofn;          // common dialog box structure
    TCHAR szFile[MAX_PATH];    // buffer for file name

    // Initialize OPENFILENAME
    ZeroMemory( &ofn, sizeof(ofn) );

    ofn.lStructSize = sizeof(ofn);
    ofn.hwndOwner = m_hWnd;
    ofn.lpstrFile = szFile;
    // Set lpstrFile[0] to '\0' so that GetOpenFileName does not 
    // use the contents of szFile to initialize itself.
    ofn.lpstrFile[0] = '\0';
    ofn.nMaxFile = sizeof( szFile );
    ofn.lpstrFilter = bXRT ? "Xort\0*.xrt\0All\0*.*\0" : "Executables\0*.exe\0All\0*.*\0";
    ofn.nFilterIndex = 1;
    ofn.lpstrFileTitle = NULL;
    ofn.nMaxFileTitle = 0;
    ofn.lpstrInitialDir = NULL;
    ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

    CString csRet;
    // Display the Open dialog box. 
    if( GetOpenFileName( &ofn ) == TRUE ) 
    {
        csRet = ofn.lpstrFile;
    }

    return csRet;
}
